package dep.dao.impl;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;


import javax.sql.DataSource;

import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class AdminDao {

    //public AdminDao() {
    public  Document getUser() throws SQLException, IOException {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        FileWriter fw = null;
        Document doc = null;
        try {
            DataSource ds = new ComboPooledDataSource();
            conn = ds.getConnection();

            //创建一个空的Document对象
            doc = DocumentHelper.createDocument();
            //添加注释
            doc.addComment("芝麻优购用户信息");
            //添加处理指令结点
            doc.addProcessingInstruction("xml-stylesheet","type='text/xsl'href='users.xsl'");
            //添加根元素user并在根元素下添加子元素
            Element users = doc.addElement("users");

            String sql = "select * from tab_user";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery(sql);
            while (rs.next()){
                Element user = users.addElement("user");

                Integer uid = rs.getInt("uid");
                String username1 = rs.getString("username");
                String password1 = rs.getString("password");
                String email1 = rs.getString("email");
                String status1 = rs.getString("status");
                String code1 = rs.getString("code");
                user.addAttribute("uid",uid.toString());
                //创建元素
                Element username = user.addElement("username");
                Element password = user.addElement("password");
                Element email = user.addElement("email");
                Element status = user.addElement("status");
                Element code = user.addElement("code");
                //为元素设置内容
                username.setText(username1);
                password.setText(password1);
                email.setText(email1);
                status.setText(status1);
                code.setText(code1);

                //格式化输出
                OutputFormat outputFormat = OutputFormat.createPrettyPrint();
                outputFormat.setEncoding("GBK");
                //输出到文件
                fw = new FileWriter("C:\\Users\\Administrator.SC-201902031211\\Desktop\\user.xml");
                XMLWriter xmlWriter = new XMLWriter(fw, outputFormat);
                xmlWriter.write(doc);
            }

        } catch (Exception e) {
            System.out.println(e);
        } finally {
            if (conn!=null){
                conn.close();
            }
            if (pstmt!=null){
                pstmt.close();
            }
            if (rs!=null){
                rs.close();
            }
            if(fw!=null){
                fw.close();
            }
        }
        return doc;
    }

}






